﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace Stone.Library.DB
{
    class ConnFactory
    {
        public static SqlConnection getConnection()
        {
            return createConnection();
        }

        private static SqlConnection createConnection()
        {
            System.Data.SqlClient.SqlConnection conn = new SqlConnection(); 
            conn.ConnectionString = "Data Source=(local); Initial Catalog=CourseArranging;Integrated Security=True;"; 
            //\\SQL2005\\SQLEXPRES
            return conn;
        }

        public static void openConn(SqlConnection conn)
        {
            if ((conn != null) && conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
        }

        public static void closeConn(SqlConnection conn)
        {
            if ((conn != null) && conn.State != ConnectionState.Closed)
            {
                conn.Close();
            }
        }

        //判断是否能够正常连接
        public static bool connState()
        {
            System.Data.SqlClient.SqlConnection conn = ConnFactory.getConnection();
            try
            {
                conn.Open();
                if (conn.State != ConnectionState.Open)
                {
                    ConnVarible.connStr = "";
                    return false;
                }
            }
            catch (Exception)
            {
                ConnVarible.connStr = "";
                return false;
            }
            finally
            {
                ConnFactory.closeConn(conn);
            }
            return true;
        }
    }
}
